/*
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.pih.javarosa.activity.entry;

import java.util.Date;
import java.util.Vector;

import org.javarosa.core.model.data.IAnswerData;
import org.javarosa.core.model.data.helper.Selection;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.utils.IInstanceProcessor;
import org.javarosa.core.services.storage.IStorageUtility;
import org.javarosa.core.services.storage.StorageFullException;
import org.javarosa.core.services.storage.StorageManager;
import org.javarosa.model.xform.XPathReference;
import org.javarosa.patient.model.Patient;
import org.javarosa.patient.model.data.NumericListData;
import org.javarosa.patient.util.DateValueTuple;
import org.pih.javarosa.model.Drug;

public class DrugEntryModelProcessor implements IInstanceProcessor {
	
	int drugId;

	public void processInstance(FormInstance tree) {
		drugId = -1;
		
		Vector drugRefs = tree.expandReference(newRef("/drugs/drug"));
		for (int i = 0; i < drugRefs.size(); i++) {
			int newDrugRef = parseDrugFromModel(tree, (TreeReference)drugRefs.elementAt(i));
			if (drugId == -1)
				drugId = newDrugRef;
		} 
	}
	
	public int parseDrugFromModel(FormInstance tree, TreeReference drugRef) {
		Drug drug = new Drug();
		
		String id = (String)getValue("/drugs/drug/id", drugRef, tree);
		String name = (String)getValue("/drugs/drug/name", drugRef, tree);
		String protocolXFormPath = (String)getValue("/drugs/drug/protocolXFormPath", drugRef, tree);

		drug.setID(Integer.parseInt(id));
		drug.setName(name);
		drug.setProtocolXFormPath(protocolXFormPath);

//		drug.setRecord("weight", weightRecords);
		
		return writeDrug(drug);
	}
	
	private int writeDrug(Drug drug) {
		IStorageUtility drugs = StorageManager.getStorage(Drug.STORAGE_KEY);
		try {
			drugs.write(drug);
		} catch (StorageFullException e) {
			throw new RuntimeException("uh-oh, storage full [patients]"); //TODO: handle this
		}
		return drug.getID();
	}
	
	private Object getValue (String xpath, TreeReference context, FormInstance tree) {
		IAnswerData val = tree.resolveReference(newRef(xpath).contextualize(context)).getValue();
		return (val == null ? null : val.getValue());
	}
	
	private TreeReference newRef (String xpath) {
		return FormInstance.unpackReference(new XPathReference(xpath));
	}
	
	public int getDrugId() {
		return drugId;
	}
}
